package com.example.hotelback.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hotelback.entity.Params;
import com.example.hotelback.entity.Reservation;
import com.example.hotelback.entity.Room;
import com.example.hotelback.exception.CustomException;
import com.example.hotelback.mapper.ReservationMapper;
import com.example.hotelback.mapper.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;

@Service
public class RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private ReservationMapper reservationMapper;

    public List<Room> findAll() {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isDelete", 0);
        return roomMapper.selectList(queryWrapper);
    }

    // 分页查询
    public IPage<Room> findByPage(Params params) {
        Page<Room> page = new Page<>(params.getCurrent(), params.getSize());
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isDelete", 0);
        IPage<Room> iPage = roomMapper.selectPage(page, queryWrapper); // 查全部
        return iPage;
    }

    // 搜索查询
    public IPage<Room> findBySearch(Params params) {
        Page<Room> page = new Page<>(params.getCurrent(), params.getSize());
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        if(params.getType() != null) {
            queryWrapper.eq("type", params.getType());
        }
        if(params.getPeopleNum() != null) {
            queryWrapper.or(wrapper -> wrapper.eq("peopleNum", params.getPeopleNum()));
        }
        queryWrapper.eq("isDelete", 0);
        IPage<Room> iPage = roomMapper.selectPage(page, queryWrapper);
        return iPage;
    }

    // 删除房型
    public void deleteRoom(int id) {
        Room room = roomMapper.selectById(id);
        room.setIsDelete(1);
        roomMapper.updateById(room); // *把更改内容持久化到数据库中；相当于sql语句
        QueryWrapper<Reservation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rid", id);
        List<Reservation> lists = reservationMapper.selectList(queryWrapper);
        for(Reservation reservation : lists) { // mybatis-plus不直接提供批量更新
            reservation.setIsDelete(1);
            reservationMapper.updateById(reservation);
        }
    }

    // 新增房型
    public void addRoom(Room room) {
        room.setIsDelete(0);
        room.setReserveRoom(0);
        roomMapper.insert(room);
    }

    public void updateRoom(int id, Room newRoom) {
        Room room = roomMapper.selectById(id); // 获取原room
        // 利用反射获取成员变量
        Field[] fields = Room.class.getDeclaredFields();
        // 动态确定是否更新
        boolean hasUpdate = false;
        for (Field field : fields) {
            field.setAccessible(true); // 暴力反射，忽略权限修饰符
            Object o1 = null; // 新
            Object o2 = null; // 原
            try {
                o1 = field.get(newRoom);
                o2 = field.get(room);
            } catch (IllegalAccessException e) {
                throw new CustomException(e.getMessage());
            }
            try {
                if (o1 != null && !o1.equals(o2)) {
                    field.set(room, o1);
                    hasUpdate = true;
                }
            } catch (IllegalAccessException e) {
                throw new CustomException(e.getMessage());
            }
        }
        // 如果有字段更新
        if(hasUpdate) {
            roomMapper.updateById(room);
        }
    }

    public Room findOneRoom(int id) {
        Room room = roomMapper.selectById(id);
        return room;
    }
}
